<?

function getAllProduct($sql=1) {
    $sql = pager_query("SELECT * FROM {product_shop} LEFT JOIN {users} ON product_uid = uid LEFT JOIN {category_brand} ON product_brand = brand_id WHERE " . $sql, 20);
    if ($sql) {
        while ($row = db_fetch_array($sql)) {
            $rs[] = $row;
        }
        return $rs;
    } else {
        echo "Câu lệnh join chưa đúng";
    }
}

function getAllCate($sql=1) {
    //echo "SELECT * FROM {category} WHERE " . $sql;
    $db = db_query("SELECT * FROM {category} WHERE " . $sql);
    if ($db) {
        while ($row = db_fetch_array($db)) {
            $rs[$row["cate_id"]] = $row["cate_name"];
        }
    } else {
        echo "Khong thực hiện được câu lệnh sql <br />";
    }
    return $rs;
}

function getAllBrand($id) {
    $sql = db_query("SELECT * FROM {category_brand} WHERE cate_id = %d", $id);
    if ($sql) {
        while ($row = db_fetch_array($sql)) {
            $rs[$row["brand_id"]] = $row["brand_name"];
        }
    } else {
        echo "Khong thực hiện được câu lệnh sql <br />";
    }
    return $rs;
}

function getPrice($id) {
    $sql = db_query("SELECT * FROM {category_filter_price} WHERE cate_id = %d", $id);
    if ($sql) {
        while ($row = db_fetch_array($sql)) {
            $rs[$row["cprice_id"]] = $row["cprice_name"];
        }
    } else {
        echo "Khong thực hiện được câu lệnh sql <br />";
    }
    return $rs;
}

function insertProduct($table, $arrayType, $arrayValue) {
    $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', array_keys($arrayType)) . ') VALUES (' . implode(', ', $arrayType) . ')';
    return db_query($sql, $arrayValue);
}

function updateProduct($table, $arrayType, $arrayValue, $id) {
    $sql = 'UPDATE ' . $table . ' SET ';
    foreach ($arrayType as $key => $value) {
        $sql .= '' . $key . ' = ' . $value . ",";
    }
    $sql = substr($sql, 0, -1);
    $sql .= " WHERE product_id = " . $id;
    return db_query($sql, $arrayValue);
}

function getExtension($filename) {
    $sExtension = substr($filename, (strrpos($filename, ".") + 1));
    $sExtension = strtolower($sExtension);
    return $sExtension;
}

function getPicName($old_name) {
    return chr(rand(97, 122)) . chr(rand(97, 122)) . chr(rand(97, 122)) . time() . "." . getExtension($old_name);
    ;
}

function deleteProduct($table, $id) {
    $sql = "SELECT * FROM " . $table . " WHERE product_id = %d";
    $rs = db_query($sql, $id);
    $row = db_fetch_array($rs);
    @unlink("files/products/" . $row["product_image"]);
    $sql = "DELETE FROM " . $table . " WHERE product_id = %d";
    return db_query($sql, $id);
}

function getResultTable($table, $sql="1") {
    $rs = array();
    $db = db_query("SELECT * FROM " . $table . "  WHERE " . $sql);
    if ($db) {
        while ($row = db_fetch_array($db)) {
            $rs[] = $row;
        }
        return $rs;
    } else {
        echo "Câu lệnh sql chưa đúng";
    }
}

function getProductShopProperty($id) {
    $rs = array();
    $db = db_query("SELECT * FROM {product_shop_property} WHERE product_id = %d", $id);
    if ($db) {
        while ($row = db_fetch_array($db)) {
            $rs[] = $row;
            $rs["val_" . $row["filter_id"]] = $row["property_name"];
        }
        return $rs;
    } else {
        echo "Câu lệnh sql chưa đúng";
    }
}

function delete_gallery($id) {
    db_query("DELETE FROM {gallery_product} WHERE gid = %d", $id);
    drupal_goto('backend/list_pictures');
}

function checkCategory($id) {
    $db = db_query("SELECT * FROM {category} WHERE cate_id = %d", $id);
    $row = db_fetch_array($db);
    if ($row) {
        return true;
    } else {
        return false;
    }
}

function getProduct($id) {
    $sql = db_query("SELECT * FROM {product_shop} WHERE product_id = %d", $id);
    $row = db_fetch_array($sql);
    return $row;
}

function getValue($value_name, $data_type = "int", $method = "GET", $default_value = 0) {
    $value = $default_value;
    switch ($method) {
        case "GET": if (isset($_GET[$value_name]))
                $value = $_GET[$value_name]; break;
        case "POST": if (isset($_POST[$value_name]))
                $value = $_POST[$value_name]; break;
        default: if (isset($_GET[$value_name]))
                $value = $_GET[$value_name]; break;
    }
    $valueArray = array("int" => intval($value), "str" => trim(strval($value)), "flo" => floatval($value), "dbl" => doubleval($value), "arr" => $value);
    foreach ($valueArray as $key => $returnResult) {
        if ($key == $data_type) {
            return $returnResult;
        }
    }
    return 0;
}

function createConbobox($name = 'danhmuc',$array_cate, $first_option = 'Vui lòng chọn') {
    $str = '<select name="'. $name . '" id="'. $name . '">';
    $str.= '<option value="">' . $first_option  . '</option>';
    foreach ($array_cate as $cate) {
        $str.= '<option value="' .$cate->cate_id  . '">' . $cate->cate_name  . '</option>';
    }
    $str .= '</select>';
    return $str;
}


function createSelect($id, $name, $array_value, $current_value, $title = '', $size=0, $onchange = '', $add_html = '') {
    $str = '<select id="' . $id . '" name="' . $name . '" class="" onchange="' . $onchange . '" size=' . $size . '>';
    ($title != '') ? $str .= '<option value=""> -- ' . $title . ' -- </option>' : $str .='';
    foreach ($array_value as $key => $value) {
        $str .= '<option value="' . $key . '"' . ($key == $current_value ? ' selected="selected"' : '') . '>' . $value . '</option>';
    }
    $str .= '</select>';
    echo $str;
}

function getAttributes($cate_id) {
    $sql = "SELECT * FROM {category_filter_name} LEFT JOIN {category_filter_group} ON filter_group = group_id WHERE {category_filter_group}.cate_id = %d";
    $db = db_query($sql, $cate_id);
    while ($row = db_fetch_array($db)) {
        $rs[$row['group_id']]["key"][$row['filter_id']] = $row;
        $rs[$row['group_id']]["value"] = $row['group_name'];
    }
    return $rs;
}

function getAttributesValue($id) {
    $sql = "SELECT * FROM {category_filter_property} WHERE filter_id = %d";
    $db_ex = db_query($sql, $id);
    while ($row = db_fetch_array($db_ex)) {
        $rs[] = $row;
    }
    return $rs;
}

function getNumber($string) {
    $number = preg_replace('/[^0-9]/', '', $string);
    if ($number == '') {
        $number = 0;
    }
    return $number = intval($number);
}

function deleteRow($table, $field_id, $field_value) {
    $sql = "DELETE FROM " . $table . " WHERE " . $field_id . " = %d";
    return db_query($sql, $field_value);
}

function getPictureByProductId($pid) {
    $query = "SELECT * FROM {files} WHERE pid = %d";
    $result = db_query($query, $pid);
    while(($obj = db_fetch_array($result)) != null) {
        $rs[] = $obj;
    }
    return $rs;
}